package com.koron.order.common.tools;

import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.DateAndTimeUtils;
import com.koron.order.common.bean.base.TrafficLight;
import com.koron.order.defect.bean.po.DefectStatus;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;

/*
 * 功能描述 获取工单编码
 * @author zhouj
 * @date 2021/10/21 11:35
 */
public class CommonCodeTools {

    /**
     * 获取特殊编号(缺陷降级) 默认长度2
     * QXTY20210923001_JJ01
     */
    public static String getNormalOrderCode(String prefix, OrderCodeEnum codeEnum) {
        return getNormalOrderCode(prefix, codeEnum, 2);
    }

    /**
     * 获取特殊编号(缺陷降级)
     * QXTY20210923001_JJ01
     */
    public static String getNormalOrderCode(String prefix, OrderCodeEnum codeEnum, int length) {
        EamUser eamUser = ThreadLocalContext.get();
        String orgPrefix = eamUser.getPrefix();
        if (orgPrefix == null) {
            orgPrefix = "";
        }
        return CodeTools.getCurrAppCodeNoWithNoDate(prefix + "_" + codeEnum.getValue(), "y", length);
    }

    /**
     * 获取工单编号
     * QXTY20210923001
     */
    public static String getNormalOrderCode(OrderCodeEnum codeEnum) {
        EamUser eamUser = ThreadLocalContext.get();
        String orgPrefix = eamUser.getPrefix();
        if (orgPrefix == null) {
            orgPrefix = "";
        }
        return CodeTools.getCurrAppCodeNo(codeEnum.getValue() + orgPrefix, "yyyyMMdd", 3);
    }


    public static String getOrderCode(OrderCodeEnum codeEnum, String preFix) {
        return CodeTools.getCurrAppCodeNo(codeEnum.getValue() + preFix, "yyyyMMdd", 3);
    }

    /*
     * 功能描述 红绿灯
     * @author zhouj
     * @date 2021/11/10 15:00
     * @param [factEndTime, planStartTime, planEndTime, status]
     * @return java.lang.String
     */
    public static String calcTrafficLight(Date factEndTime, Date planStartTime, Date planEndTime, String status) {

        if (!ObjectUtils.allNotNull(planStartTime, planEndTime)) {
            return TrafficLight.Normal.getCode();
        }

        //工期
        long startTime = planStartTime.getTime();
        long endTime = planEndTime.getTime();

        long time = endTime - startTime;

        BigDecimal bigDecimal = DateAndTimeUtils.longtimeToHour(2, time);
        //工期*0.2 工期*20%计算的结果精确到小时，向上取整，比如0.3小时算1小时
        BigDecimal res_02 = bigDecimal.multiply(new BigDecimal("0.2")).setScale(0, BigDecimal.ROUND_UP);

        long currentTime = System.currentTimeMillis();
        long time1 = endTime - currentTime;
        BigDecimal bigDecimal1 = DateAndTimeUtils.longtimeToHour(0, time1);

        double v_02 = res_02.doubleValue();
        double v1 = bigDecimal1.doubleValue();

        boolean equals = StringUtils.equals(DefectStatus.ARCHIVED.getStatus(), status);

        //根据时间来判断红路灯

        if ((!equals && (v1 > v_02)) || (equals && (factEndTime != null) && endTime >= factEndTime.getTime())) {
            return TrafficLight.Normal.getCode();
        } else if (!equals && endTime > currentTime && v1 <= v_02) {
            return TrafficLight.Warn.getCode();
        } else if ((!equals && currentTime >= endTime) || (factEndTime != null && equals && factEndTime.getTime() > endTime)) {
            return TrafficLight.OverTime.getCode();
        } else {
            return TrafficLight.Normal.getCode();
        }
    }

    /**
     * 获取试验工单报告页尾的编号
     * QXTY20210923001_JJ01
     */
    public static String getExperimentReportCode(String prefix, int length) {
        return CodeTools.getCurrAppCodeNo(prefix, "yyyy", length);
    }

}
